package KFMmovie;

import java.util.Objects;

public class MovieTheater {
    private static Movie[] arr;
    private static User[] arr1;

    private int userCount;//用户下标private
    private int movieCount;//电影下标

    /**
     * 用户与电影数组
     */
    public MovieTheater() {
        arr = new Movie[5];
        arr1 = new User[2];
        arr1[0] = new User("admin","123456","123456",1);
        arr1[1] = new User("zcj", "18193462990", "123456", 2);
        userCount = 2;
        arr[0] = new Movie("消失的她", 45.6, "海底星空", 20230619, 80);
        arr[1] = new Movie("孤注一掷", 59, "忘了", 20230811, 55);
        arr[2] = new Movie("速度与激情9", 48.9, "奥斯汀格里芬", 20220516, 45);
        arr[3] = new Movie("大闹天宫", 39, "王宝强", 20220619, 50);
        arr[4] = new Movie("釜山行", 49, "车银优", 20200328, 60);
        movieCount = 5;
    }

    /***
     * 电影扩容
     */

    private void movieGroup() {
        if (movieCount == arr.length) {
            Movie[] newMovies = new Movie[arr.length * 2];
            System.arraycopy(arr, 0, newMovies, 0, arr.length);
            arr = newMovies;
        }
    }

    /***
     * 用户扩容
     */

    public void userGrow() {
        if (userCount == arr1.length) { // 说明已经存储到极限了，此时要扩容
            User[] newusers = new User[arr1.length * 2];
            System.arraycopy(arr1, 0, newusers, 0, arr1.length);
            arr1 = newusers;
        }
    }

    /***
     * 查询电影
     * @param name 输入电影名称
     * @return
     */
    public Movie selectMovie(String name) {
        if (name == null) {
            return null;
        }
        for (int i = 0; i < movieCount; i++) {
            if (arr[i].getName().equals(name)) {
                return arr[i];
            }
        }
        return null;
    }

    /**
     * 显示所有电影
     */
    public void showMovies() {
        if (movieCount == 0) {
            System.out.println("暂时没有电影。");
            return;
        }
        for (int i = 0; i < movieCount; i++) {
            System.out.println(arr[i]);
        }
    }

    /**
     * 按上映日期排序输出
     */
    public void sortMovieByDate() {
        // 排序 movies
        for (int i = 0; i < movieCount; i++) {
            int minindex = i;
            for (int j = i; j < movieCount ; j++) {
                if (arr[minindex].getPricce() > arr[j].getPricce()) {
                    minindex = j;
                }
                Movie temp = arr[j];
                arr[j] = arr[minindex];
                arr[minindex] = temp;
            }

            System.out.println(arr[i]);

        }
    }

    /**
     * 按照user对象查询用户
     *
     * @param user 要查询的用户信息
     * @return 如果存在返回用户对象，不存在返回null
     */
    public User selectUSer(User user) {
        for (int i = 0; i < userCount; i++) {
            if (Objects.equals(arr1[i].getPhone(), user.getPhone())
                    && Objects.equals(arr1[i].getPassword(), user.getPhone())
                    && arr1[i].getRole() == user.getRole()) {
                return arr1[i];
            }
        }
        return null;
    }

    /**
     * 通过用户密码、电话、身份查询用户信息
     *
     * @param psaaword 用户密码
     * @param phone    用户电话
     * @param role     用户身份
     * @return
     */
    public User selectUSer(String psaaword, String phone, int role) {
        for (int i = 0; i < userCount; i++) {
            if (Objects.equals(arr1[i].getPhone(), phone)
                    && Objects.equals(arr1[i].getPassword(), psaaword)
                    && arr1[i].getRole() == role) {
                return arr1[i];
            }
        }
        return null;
    }

    /**
     * 添加电影
     * 添加用户
     */

    public void addMovie(Movie movie) {
        movieGroup();
        arr[movieCount] = movie;
        movieCount++;
    }

    public void addUser(User user) {
        userGrow();
        arr1[userCount] = user;
        userCount++;
    }

    /**
     * 验证电话号码的正确
     *
     * @param phone
     * @return
     */
    public User selectUserByPhone(String phone) {
        for (int i = 0; i < userCount; i++) {
            if (Objects.equals(arr1[i].getPhone(), phone)) {
                return arr1[i];
            }
        }
        return null;
    }

    /**
     * 修改users数组存储的对象的值
     *
     * @param u1当前登录用户的信息
     * @param u2用户输入的新的用户信息
     * @return
     */

    public User updateUser(User u1, User u2) {
        for (int i = 0; i < userCount; i++) {
            if (arr[i].equals(u1)) {  // 查询到你要修改的是哪一个对象 users[i]
                if (u2.getPassword() != null) {  // u2 用户输入的修改内容
                    arr1[i].setPassword(u2.getPassword());  //如果输入密码
                } else if (u2.getUsername() != null) {
                    arr1[i].setUsername(u2.getUsername());  //如果输入用户名
                } else {
                    arr1[i].setPhone(u2.getPhone());  //如果输入了电话
                }
                return arr1[i];
            }
        }
        return null;
    }

    /**
     * 根据电影名称删除电影
     *
     * @param name 输入的电影名称
     * @return 是否删除成功
     */
    public boolean deleteMovie(String name) {
        int index = -1;
        for (int i = 0; i < movieCount; i++) {
            if (Objects.equals(name, arr[i].getName())) {
                index = i;
            }
        }
        if (index == -1) {
            return false;
        }
        System.arraycopy(arr, 0, arr, 0, index);
        System.arraycopy(arr, index + 1, arr, index, movieCount - index - 1);
        movieCount--;
        return true;
    }

    /**
     * 票数
     */
    public int selectMovieBtName(String name) {
        for (int i = 0; i < movieCount; i++) {
            if (arr[i].getName().equals(name)) {
                return i;
            }
        }
        return -1;
    }

    public Movie buyMovieByName(String name) {
        Movie movie = arr[selectMovieBtName(name)];
        System.out.println(movie);
        return movie;
    }
}

